import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class RemoteIOCopy {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.err.println("Invalid parameters list. Usage: RemoteIOCopy <source> <remote host> <remote port> ");
			System.exit(1);
		}
		String src = args[0];
		String host = args[1];
		int port = Integer.parseInt(args[2]);
		
		Copier copier = new StreamCopier();
		long before = System.currentTimeMillis();
		try (Socket socket = new Socket(host, port);
				InputStream is = new BufferedInputStream(new FileInputStream(src));
				DataOutputStream os = new DataOutputStream(socket.getOutputStream())) {
			os.flush();
			os.writeInt((int) new File(src).length());
			copier.copy(is, os, 100000);
			os.flush();
			InputStream in = socket.getInputStream();
			in.read();
		}
		long after = System.currentTimeMillis();
		System.out.print("Net IO : ");
		System.out.println(after - before);
		
	}
}
